package com.ywger.service.exception;


import com.ywger.service.constants.ResultCode;
import lombok.Data;

/**
 * @author chenmeng
 * @date 2018-12-11 14:39
 * @desc 统一业务异常返回
 */
@Data
public class BizException extends RuntimeException {

    private String resultCode;
    private String resultMessage;

    private BizException(ResultCode resultCode) {
        this(resultCode.name(), resultCode.getDesc());
    }

    private BizException(ResultCode resultCode,Throwable throwable) {
        this(resultCode.name(), resultCode.getDesc(),throwable);
    }

    private BizException(String resultCode, String resultMessage) {
        super("[" + resultCode + ":" + resultCode + "]");
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;

    }

    /**
     * 构造自定义业务异常，传递原始异常
     * @param resultCode
     * @param resultMessage
     * @param throwable
     */
    private BizException(String resultCode, String resultMessage, Throwable throwable) {

        super("[" + resultCode + ":" + resultCode + "]", throwable);
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;
    }

    /**
     *  通过 ResultCode 构造自定异常
     * @param resultCode
     * @return
     */
    public static BizException of(ResultCode resultCode) {
        return new BizException(resultCode);
    }

    /**
     * 通过 参数 构造自定义异常
     * @param resultCode
     * @param resultMessage
     * @return
     */
    public static BizException of(String resultCode, String resultMessage) {
        return new BizException(resultCode, resultMessage);
    }

    /**
     * 构造自定义异常，传递原始异常
     * @param resultCode
     * @param resultMessage
     * @param throwable
     * @return
     */
    public static BizException of(String resultCode, String resultMessage, Exception throwable) {
        return new BizException(resultCode, resultMessage, throwable);
    }

    public static BizException of(ResultCode resultCode, Exception throwable) {
        return new BizException(resultCode, throwable);
    }

}
